package com.seari.custody.service.Impl;


import com.seari.custody.constant.MessageConstant;
import com.seari.custody.mapper.MeetingStatisticalMapper;
import com.seari.custody.pojo.MessageBean;
import com.seari.custody.service.MeetingStatisticalService;
import com.seari.custody.util.HandlerNullUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MeetingStatisticalServiceImpl implements MeetingStatisticalService {

    @Autowired
    private MeetingStatisticalMapper meetingStatisticalMapper;


    @Override
    public Integer querySumMeeting(String startTime,String endTime) {
        return meetingStatisticalMapper.querySumMeeting( startTime, endTime);
    }

    @Override
    public Integer querySumMeetingFile(String startTime, String endTime) {
        return meetingStatisticalMapper.querySumMeetingFile( startTime,  endTime);
    }

    @Override
    public List<Map<String, String>> queryMeetingTime(String startTime,String endTime) {
        return meetingStatisticalMapper.queryMeetingTime( startTime, endTime);
    }

    @Override
    public Integer querySumUser() {
        return meetingStatisticalMapper.querySumUser();
    }

    @Override
    public Integer querySumUserforMeeting(String startTime,String endTime) {
        return meetingStatisticalMapper.querySumUserforMeeting( startTime, endTime);
    }

    @Override
    public Map<String, Object> queryCountMeeting(String startTime, String endTime) {
        Map<String, Object> dataMap = new LinkedHashMap<>();
        try {
            //会议总数
            Integer sumMeeting=meetingStatisticalMapper.querySumMeeting(startTime,endTime);
            dataMap.put("sumMeeting", sumMeeting);
            //生成会议记要
            Integer sumMeetingFile=meetingStatisticalMapper.querySumMeetingFile(startTime,endTime);
            dataMap.put("sumMeetingFile", sumMeetingFile);
            //会议累计时长
            List<Map<String,String>> meetingTime=meetingStatisticalMapper.queryMeetingTime(startTime,endTime);
            long sum=0;
            for(Map<String,String> m:meetingTime){
                sum+=countTime(m.get("startTime"),m.get("endTime"));
            }
            sum=sum/(60*60);
            dataMap.put("sumMeetingTime", sum);
            //人均会议
            Integer sumUser=meetingStatisticalMapper.querySumUser();
            int avgMeeting=sumMeeting/sumUser;
            dataMap.put("avgMeeting", avgMeeting);
            //线上会议总参与人数
            Integer sumUserforMeeting=meetingStatisticalMapper.querySumUserforMeeting(startTime,endTime);
            dataMap.put("sumUserforMeeting", sumUserforMeeting);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataMap;
    }


    @Override
    public List<Map<String, String>> queryMeetingBar(List<String> typeList, String startTime, String endTime) {
        return meetingStatisticalMapper.queryMeetingBar(typeList, startTime, endTime);
    }

    /**
     * 计算间隔时间（单位：秒）
     * @return
     * @throws Exception
     */
    private static long countTime(String startTime,String endTime) throws Exception {
        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date begin=dfs.parse(startTime);
        Date end = dfs.parse(endTime);
        long between=(end.getTime()-begin.getTime())/1000;//除以1000是为了转换成秒
//        long day1=between/(24*3600);
//        long hour1=between%(24*3600)/3600;
//        long minute1=between%3600/60;
//        long second1=between%60/60;
//        System.out.println(between/(60*60)+"小时");
        return between;
    }
}
